package com.yue92.datalife.controller.v1;

import com.yue92.datalife.common.CodeEnum;
import com.yue92.datalife.common.GroupCreate;
import com.yue92.datalife.common.GroupUpdate;
import com.yue92.datalife.exception.UncheckedException;
import com.yue92.datalife.helper.RecordHelper;
import com.yue92.datalife.model.Record;
import com.yue92.datalife.model.RecordItem;
import com.yue92.datalife.model.User;
import com.yue92.datalife.request.RecordItemReq;
import com.yue92.datalife.response.*;
import com.yue92.datalife.service.IRecordItemExtraService;
import com.yue92.datalife.service.IRecordItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.*;

import static com.yue92.datalife.common.Constant.CURRENT_USER_ATTRIBUTE;

@RestController
@RequestMapping(value = "/api/v1/record-items", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class RecordItemController {

    @Autowired
    private RecordHelper recordHelper;

    @Autowired
    private IRecordItemService recordItemService;

    @Autowired
    private IRecordItemExtraService recordItemExtraService;

    @PostMapping("")
    public ResponseResource<?> create(@Validated({GroupCreate.class}) @RequestBody RecordItemReq recordItemReq, @RequestAttribute(CURRENT_USER_ATTRIBUTE) User currentUser) {
        Record record = recordHelper.checkAndGetRecord(recordItemReq.getRecordId(), currentUser);

        RecordItem recordItem = new RecordItem();
        recordItem.setRecordId(record.getId());
        recordItem.setRecordDate(LocalDate.parse(recordItemReq.getRecordDate()));
        recordItem.setValue(recordItemReq.getValue());
        recordItem.setComment(recordItemReq.getComment());

        recordItemService.save(recordItem);

        return ResponseResource.success(new HashMap<String, Object>() {{
            put("id", recordItem.getId());
        }});
    }

    @PutMapping("/{id}")
    public ResponseResource<?> update(@PathVariable("id") Long id, @Validated({GroupUpdate.class}) @RequestBody RecordItemReq recordItemReq, @RequestAttribute(CURRENT_USER_ATTRIBUTE) User currentUser) {
        Optional<RecordItem> recordItemOptional = recordItemService.get(id);
        if (!recordItemOptional.isPresent()) {
            throw new UncheckedException(CodeEnum.FAILURE, "record item not exist");
        }

        RecordItem recordItem = recordItemOptional.get();

        recordHelper.checkAndGetRecord(recordItem.getRecordId(), currentUser);

        recordItem.setValue(recordItemReq.getValue());
        recordItem.setComment(recordItemReq.getComment());

        recordItemService.save(recordItem);

        return ResponseResource.success();
    }

    @DeleteMapping("/{id}")
    public ResponseResource<?> delete(@PathVariable("id") Long id, @RequestAttribute(CURRENT_USER_ATTRIBUTE) User currentUser) {
        Optional<RecordItem> recordItemOptional = recordItemService.get(id);
        if (!recordItemOptional.isPresent()) {
            throw new UncheckedException(CodeEnum.FAILURE, "record item not exist");
        }

        RecordItem recordItem = recordItemOptional.get();

        recordHelper.checkAndGetRecord(recordItem.getRecordId(), currentUser);

        if (recordItemExtraService.existsByRecordItemId(id)) {
            throw new UncheckedException(CodeEnum.FAILURE, "has child data");
        }

        recordItemService.remove(recordItem.getId());

        return ResponseResource.success();
    }
}
